import {
  Body,
  Inject,
  Post,
  Provide,
  ALL,
  Validate,
} from '@midwayjs/decorator';
import { CreateApiDoc } from '@midwayjs/swagger/dist/lib/createAPI';
import { Context } from 'egg';
import { CoolController, BaseController } from '@cool-midway/core';
import { UserInfoService } from '../../../user/service/info';
import { WeiboBlogEntity } from '../../entity/blog';
import { WeiboBlogService } from '../../service/weibo';

/**
 * 支付示例
 */
@Provide()
@CoolController(
  {
    api: ['delete'],
    entity: WeiboBlogEntity,
    service: WeiboBlogService,
  },
  {
    tagName: '微博',
  }
)
export class WeiboUserController extends BaseController {
  @Inject()
  ctx: Context;

  @Inject()
  weiboBlogSrv: WeiboBlogService;

  @Inject()
  userSrv: UserInfoService;

  /**
   * 作品
   */
  @CreateApiDoc()
    .summary('我的作品')
    .param('sn', { required: true })
    .respond(200)
    .build()
  @Post('/works')
  @Validate()
  async works(@Body(ALL) query: any) {
    const user = await this.userSrv.getSn(query.sn);

    const data = await this.weiboBlogSrv.page(
      Object.assign(query, { userId: user?.id }, { my: 'type' })
    );
    data.list = await this.weiboBlogSrv.transformInLove(data.list, user?.id);
    return this.ok(data);
  }

  @CreateApiDoc()
    .summary('用户的喜欢')
    .param('sn', { required: true })
    .respond(200)
    .build()
  @Post('/love')
  @Validate()
  async love(@Body(ALL) query: any) {
    const user = await this.userSrv.getSn(query.sn);
    console.log('user===>', user);

    const data = await this.weiboBlogSrv.inLove(user.id, query);
    data.list = await this.weiboBlogSrv.transformInLove(data.list, user?.id);
    return this.ok(data);
  }

  @CreateApiDoc()
    .summary('用户的收藏')
    .param('sn', { required: true })
    .respond(200)
    .build()
  @Post('/collect')
  @Validate()
  async collect(@Body(ALL) query: any) {
    const user = await this.userSrv.getSn(query.sn);
    console.log('user===>', user);

    const data = await this.weiboBlogSrv.inCollect(user.id, query);
    data.list = await this.weiboBlogSrv.transformInLove(data.list, user?.id);
    return this.ok(data);
  }

  @CreateApiDoc()
    .summary('用户的排行榜')
    .param('order', { required: true })
    .param('size')
    .respond(200)
    .build()
  @Post('/ranking')
  @Validate()
  async ranking(@Body(ALL) query: any) {
    
    // 找出点赞前三的用户
    const data = await this.weiboBlogSrv.page({
      ...query,
      groupBy: 'a.user_id',
    });
    data.list = await this.userSrv.transformInUser(data.list, 'user_id');
    await Promise.all(
      data.list.map(async (item,index) => {
        // 找出这个用户的所有作品
        const worksList = await this.weiboBlogSrv.page({
          userId: item.user_id,
          size: 999,
          my:'type'
        });

        //  计算热度 （点赞数）
        const raking = worksList.list.reduce((pre, cur) => {
          return  pre + cur.count_love;
        }, 0);
        item.user['hot_ranking'] =raking
        item['hot_ranking'] =raking

      })
    );

    return this.ok(data);
  }
}
